package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.controller.BaseController;
import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzPagination;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.domain.RmpUser;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.services.RmpDeptService;
import com.youthburin.rmp.services.RmpUserService;

@Controller
public class RmpUserController extends BaseController {
	@Autowired
	private RmpUserService rmpUserService;
	@Autowired
	private RmpDeptService rmpDeptService;
	@Autowired
	@Qualifier("rmpOpLogService")
	private OpLogService opLogService;

	@RequiresPermissions("user:list")
	@RequestMapping(value = "/user/list")
	public String list(HttpServletRequest request) {
		opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_USER.toString());
		rmpUserService.dwzFindAll((DwzPagination) request.getAttribute(DwzPagination.DWZPAGINATION_PROPERTY_KEY));
		return "user/list";
	}

	@RequiresPermissions("user:add")
	@RequestMapping(value = "/user/add", method = RequestMethod.GET)
	public String toAdd() {
		return "user/edit";
	}

	@RequiresPermissions("user:add")
	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse add(@Valid RmpUser rmpUser, BindingResult bindingResult,
			@RequestParam("dept.code") String deptCode, HttpServletRequest request) {
		if (bindingResult.hasErrors()) {
			return DwzResponseUtils.createFailResponse(bindingResult.getAllErrors());
		} else {
			if (StringUtils.equals("0", deptCode)) {
				return DwzResponseUtils.createFailResponse(getMessage("error.user.rootdeptsetusererror", request));
			}
			rmpUser.setRmpDept(rmpDeptService.get(deptCode));
			RmpUser newRmpUser = rmpUserService.save(rmpUser);
			opLogService.addLog(RmpOpLogTypeEnum.ADD.toString(), RmpOpLogBusiTypeEnum.RMP_USER.toString(),
					newRmpUser.getCode(), newRmpUser.getName());
			return DwzResponseUtils.createSuccessResponse(true, "user_panel");
		}
	}

	@RequiresPermissions("user:edit")
	@RequestMapping(value = "/user/edit/{userId}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("userId") String userId, ModelMap model) {
		RmpUser user = rmpUserService.get(userId);
		model.put("user", user);
		model.put("deptCode", user.getRmpDept().getCode());
		model.put("deptName", user.getRmpDept().getName());

		return "user/edit";
	}

	@RequiresPermissions("user:view")
	@RequestMapping(value = "/user/view/{userId}", method = RequestMethod.GET)
	public String toView(@PathVariable("userId") String userId, ModelMap model) {
		RmpUser user = rmpUserService.get(userId);
		opLogService.addLog(RmpOpLogTypeEnum.VIEW.toString(), RmpOpLogBusiTypeEnum.RMP_USER.toString(), user.getCode(),
				user.getName());
		model.put("user", user);
		model.put("deptCode", user.getRmpDept().getCode());
		model.put("deptName", user.getRmpDept().getName());
		model.put("readOnly", "1");

		return "user/edit";
	}

	@RequiresPermissions("user:edit")
	@RequestMapping(value = "/user/edit", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse edit(RmpUser rmpUser, @RequestParam("dept.code") String deptCode, HttpServletRequest request) {
		if (StringUtils.equals("0", deptCode)) {
			return DwzResponseUtils.createFailResponse(getMessage("error.user.rootdeptsetusererror", request));
		}
		rmpUser.setRmpDept(rmpDeptService.get(deptCode));
		rmpUserService.update(rmpUser);

		opLogService.addLog(RmpOpLogTypeEnum.UPDATE.toString(), RmpOpLogBusiTypeEnum.RMP_USER.toString(),
				rmpUser.getCode(), rmpUser.getName());

		return DwzResponseUtils.createSuccessResponse(true, "user_panel");
	}

	@RequiresPermissions("user:delete")
	@RequestMapping(value = "/user/delete", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			List<String> idList = new ArrayList<String>();
			for (String id : ids.split(",")) {
				if (StringUtils.isNotBlank(id)) {
					idList.add(StringUtils.trim(id));
				}
			}
			rmpUserService.delete(idList);

			for (String id : idList) {
				opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_USER.toString(), id);
			}
		}
		return DwzResponseUtils.createSuccessResponse(false);
	}

	@RequestMapping(value = "/user/validateCode")
	@ResponseBody
	public String validateCode(String code) {
		if (rmpUserService.get(code) == null) {
			return "1";
		} else {
			return "0";
		}
	}
}
